package com.sztx.pay.center.rpc.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.Constance;
import com.sztx.pay.center.common.util.Constance.ActLevel;
import com.sztx.pay.center.common.util.Constance.BindCardType;
import com.sztx.pay.center.common.util.Constance.CardActType;
import com.sztx.pay.center.common.util.Constance.CardType;
import com.sztx.pay.center.common.util.Constance.PayFlowState;
import com.sztx.pay.center.common.util.Constance.PayFlowType;
import com.sztx.pay.center.common.util.Constance.UserActType;
import com.sztx.pay.center.common.util.Constance.UserType;
import com.sztx.pay.center.common.util.ObjectUtil;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.common.yhbf.YHBFSensitiveEnc;
import com.sztx.pay.center.core.service.AccountService;
import com.sztx.pay.center.core.service.ActService;
import com.sztx.pay.center.core.service.BankUtilService;
import com.sztx.pay.center.core.service.OrderMappingService;
import com.sztx.pay.center.core.service.PayCoreService;
import com.sztx.pay.center.core.service.UserInfoService;
import com.sztx.pay.center.core.service.bean.BindInfo;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserBindCardMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserInfoMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.PlatUserInfoDO;
import com.sztx.pay.center.dataaccess.domain.UserActDO;
import com.sztx.pay.center.dataaccess.domain.UserBindCardDO;
import com.sztx.pay.center.dataaccess.domain.UserInfoDO;
import com.sztx.pay.center.rpc.api.domain.ActBanlanceResponse;
import com.sztx.pay.center.rpc.api.domain.BindCardInfo;
import com.sztx.pay.center.rpc.api.domain.BindCardRequest;
import com.sztx.pay.center.rpc.api.domain.PageModelResponse;
import com.sztx.pay.center.rpc.api.domain.QueryBanlanceListResponse;
import com.sztx.pay.center.rpc.api.domain.QueryMerBindCardRequest;
import com.sztx.pay.center.rpc.api.domain.ResponsePageModel;
import com.sztx.pay.center.rpc.api.domain.TradeResponse;
import com.sztx.pay.center.rpc.api.domain.UserAccountInfoResponse;
import com.sztx.pay.center.rpc.api.domain.UserActInfo;
import com.sztx.pay.center.rpc.api.domain.UserActInfoResponse;
import com.sztx.pay.center.rpc.api.domain.UserInfoRequest;
import com.sztx.pay.center.rpc.api.domain.UserInfoResponse;
import com.sztx.pay.center.rpc.api.domain.VerifyQuickPayRequest;
import com.sztx.pay.center.rpc.api.service.AccountRpcService;
import com.sztx.pay.center.rpc.component.VerifyQuickPayService;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.common.util.RegexUtil;
import com.sztx.se.common.util.StringUtil;
import com.sztx.se.core.context.SpringContextHolder;
import com.sztx.se.core.service.impl.BaseServiceImpl;
import com.sztx.se.dataaccess.mysql.config.PageQuery;
import com.sztx.usercenter.rpc.api.domain.out.UserStatusInfoVO;

@Service
public class AccountRpcServiceImpl extends BaseServiceImpl implements AccountRpcService {
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private UserBindCardMysqlDAO userBindCardMysqlDAO;
	
	@Autowired
	private BankUtilService bankUtilService;
	
	@Autowired
	private ActService actService;
	
	@Autowired
	UserActMysqlDAO userActMysqlDAO;
	
	@Autowired
	UserInfoMysqlDAO userInfoMysqlDAO;
	
	List<String> grayUserList = new ArrayList<String>();
	
	@Autowired
	private UserInfoService userInfoService;
	
	@Autowired
	OrderMappingService orderMappingService;
	
	@Autowired
	PayCoreService payCoreService;
	
	@Override
	public String createAct(String userId, Integer userType, Integer actType, String mobileNo, String attach) {
		
		LOG.debug("createAct");
		
		return accountService.createAct(userId, userType, actType, mobileNo, attach);
		
	}

	@Override
	@SlaveQueryAnnotation
	public int queryBalance(String userId, Integer actType) {
		LOG.debug("queryBalance");
		long banlance = accountService.queryBalance(userId, actType,false);
		return (int)banlance;
	
	}
	
	@Override
	@SlaveQueryAnnotation
	public ActBanlanceResponse queryAllBalance(String userId, Integer actType) {
		LOG.debug("queryAllBalance");
		ActBanlanceResponse response = new ActBanlanceResponse();
		UserActDO userActDO = accountService.queryAllBalance(userId, actType,false);
		if(userActDO != null){
			response.setActBalance(userActDO.getActBalance());
			response.setFreezeBalance(userActDO.getFreezeBalance());
		}

		return response;
	
	}

	@Override
	public boolean freezedBalance(String userId, Integer actType, String operator, String attach) {
		LOG.debug("freezedBalance");
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		ValidateUtil.isNotBlank(operator, "operator不能为空");
		ValidateUtil.notNull(actType, "actType不能为空");
		
		if (StringUtil.isNotBlank(attach)) {
			ValidateUtil.le(attach.length(), 255, "attach不能超过255个字符");
		}
		
		return accountService.freezedBalance(userId, actType, operator, attach);
	}

	@Override
	public boolean unfreezeBalance(String userId, Integer actType, String operator, String attach) {
		LOG.debug("unfreezeBalance");
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		ValidateUtil.isNotBlank(operator, "operator不能为空");
		ValidateUtil.notNull(actType, "actType不能为空");
		
		if (StringUtil.isNotBlank(attach)) {
			ValidateUtil.le(attach.length(), 255, "attach不能超过255个字符");
		}
		

		return accountService.unfreezeBalance(userId, actType, operator, attach);
	}

	@Override
	public String bindCard(BindCardRequest request) {
		LOG.debug("bindCard");
		
		String userId = request.getUserId();
		BindCardType bindType = BindCardType.valueOf(request.getBindType());		
		CardActType cardActType = CardActType.valueOf(request.getCardActType());
		CardType cardType = CardType.valueOf(request.getCardType());
		String cardNo = request.getCardNo();
		String cvv = request.getCvv();
		String expireDate = request.getExpireDate();
		expireDate = com.sztx.pay.center.common.util.DateUtil.tranExpireDate(expireDate);
		String realName = request.getRealName();
		String identityNo = request.getIdentityNo();
		String mobileNo = request.getMobileNo();
		String cardProvince = request.getCardProvince();
		String cardCity = request.getCardCity();
		String bankAddr = request.getBankAddr();
		String channelId = request.getChannelId();
		Integer isWithdrawCard = request.getIsWithdrawCard();
		String autoRemark = request.getAutoRemark();
		String companyName = request.getCompanyName();
		String updateUser = request.getUpdateUser();
		String bankName = request.getBankName();
		String swiftCode = request.getSwiftCode();
		String gateWayChannelId = request.getGateWayChannelId();
		String routeNo = request.getRouteNo();
		String clientSourceType = request.getClientSourceType();
		
		BindInfo bindInfo = new BindInfo();
		bindInfo.setBindType(bindType);
		bindInfo.setCardActType(cardActType);
		bindInfo.setCardType(cardType);
		bindInfo.setCardNo(cardNo);
		bindInfo.setCvv(cvv);
		bindInfo.setExpireDate(expireDate);
		bindInfo.setRealName(realName);
		bindInfo.setIdentityNo(identityNo);
		bindInfo.setMobileNo(mobileNo);
		bindInfo.setCardProvince(cardProvince);
		bindInfo.setCardCity(cardCity);
		bindInfo.setBankAddr(bankAddr);
		bindInfo.setChannelId(channelId);
		bindInfo.setIsWithdrawCard(isWithdrawCard);
		bindInfo.setAutoRemark(autoRemark);
		bindInfo.setCompanyName(companyName);
		bindInfo.setUpdateUser(updateUser);
		bindInfo.setBankName(bankName);
		bindInfo.setSwiftCode(swiftCode);
		
		
		return accountService.bindCard(userId, bindInfo,gateWayChannelId,routeNo, clientSourceType);
	}

	@Override
	@SlaveQueryAnnotation
	public int queryBindCardFree(String cardNo) {
		
		return bankUtilService.queryBindCardFree(cardNo);
	}

	@Override
	public boolean unbindCard(String userId, String bindNo) {
		
		return accountService.unbindCard(userId, bindNo);
	}

	@Override
	public boolean setPrimaryCard(String userId, String bindNo) {
		
		return accountService.setPrimaryCard(userId, bindNo);
	}

	@Override
	@SlaveQueryAnnotation
	public List<BindCardInfo> queryBindCardList(String userId, int bindType) {
		List<UserBindCardDO> list = accountService.queryBindCardList(userId, bindType);
		if (list == null || list.isEmpty()) {
			return null;
		}
		return transferBindResult(list);
	}
			
	@Override
	public boolean verifyBindCard(String bindNo, String verifyCode) {
		
		//return accountService.verifyBindCard(bindNo, verifyCode);
		VerifyQuickPayRequest request = new VerifyQuickPayRequest();
		request.setTradeOrderId(bindNo);
		request.setVerifyCode(verifyCode);
		
		VerifyQuickPayService service = SpringContextHolder.applicationContext.getBean("verifyQuickPayService", VerifyQuickPayService.class);
		Map<String, String> params = new HashMap<String, String>();
		params.put("tradeOrderId", bindNo);
		params.put("verifyCode", verifyCode);
		TradeResponse response = service.verify(params);
		PayFlowState state = PayFlowState.valueOf(Integer.valueOf(response.getResultCode()));
		if( state == PayFlowState.SUCCESS || state == PayFlowState.PAYING ){
			return true;
		}else{			
			String errorMsg = response.getResultCode();			
			if (StringUtil.isBlank(errorMsg)) {
				errorMsg ="[" + bindNo + "]绑卡失败";
			}
			
			throw new BusinessException(response.getResultMsg());			
		}
	}

	@Override
	public String transferUser(String userId,Integer userType, Integer isRealName,
			String mobileNo, String realName, String identityNo) {
				
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		ValidateUtil.notNull(isRealName, "isRealName不能为空");
		ValidateUtil.isBetween(isRealName, 0, 1, "isRealName参数有误");
		boolean flag = false;
		if (isRealName.intValue() == 1) {
			flag = true;
//			ValidateUtil.isNotBlank(mobileNo, "mobileNo不能为空");
			ValidateUtil.isNotBlank(realName, "realName不能为空");
			ValidateUtil.isNotBlank(identityNo, "identifyNo不能为空");
		}
		return accountService.transferUser(userId, userType, flag, mobileNo, realName, identityNo, "admin", null);	
}
	@Override
	public String transferUser(String userId,Integer userType, Integer isRealName,
			String mobileNo, String realName, String identityNo,String lastOperator,String remark) {

		ValidateUtil.isNotBlank(userId, "userId不能为空");
		ValidateUtil.notNull(isRealName, "isRealName不能为空");
		ValidateUtil.isBetween(isRealName, 0, 1, "isRealName参数有误");
		boolean flag = false;
		if (isRealName.intValue() == 1) {
			flag = true;
//			ValidateUtil.isNotBlank(mobileNo, "mobileNo不能为空");
			ValidateUtil.isNotBlank(realName, "realName不能为空");
			ValidateUtil.isNotBlank(identityNo, "identifyNo不能为空");
		}
		
		return accountService.transferUser(userId, userType,flag, mobileNo, realName, identityNo,lastOperator,remark);
	}

	@Override
	public boolean realAuth(String userId, Integer actLevel, String mobileNo,
			String realName, String identityNo) {
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		ValidateUtil.notNull(actLevel, "actLevel不能为空");
		
		if (actLevel.intValue() != ActLevel.LEVEL_V2.getCode()) {
			throw new BusinessException("现只支持用户等级为"+ActLevel.LEVEL_V2.getCode()+"的实名认证");
		}
		
//		ValidateUtil.isNotBlank(mobileNo, "mobileNo不能为空");
		ValidateUtil.isNotBlank(realName, "realName不能为空");
		ValidateUtil.isNotBlank(identityNo, "identifyNo不能为空");
		
		return accountService.realAuth(userId, ActLevel.valueOf(actLevel), realName, identityNo, mobileNo);		
	}

	@Override
	public boolean changeUserType(String userId, Integer userType, String attach) {
	
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		ValidateUtil.notNull(userType, "userType不能为空");
		
		return accountService.changeUserType(userId, userType, attach);
	}

	@Override
	public boolean resendVerifyCode(String bindNo) {
		String referOrderId = orderMappingService.getMappingOrderId(bindNo, (byte) PayFlowType.PAY.getCode());
		try {
			payCoreService.resendQuickVC(referOrderId);
			return true;
		} catch (BusinessException e) {
			return false;
		}
//		return accountService.resendVerifyCode(bindNo);
	}

	@Override
	@SlaveQueryAnnotation
	public BindCardInfo findCardInfoByBindNo(String bindNo) {
		UserBindCardDO cardDo = accountService.findUserBindInfo(bindNo);
		
		BindCardInfo cardInfo = new BindCardInfo();
		cardInfo.setBankId(cardDo.getBankId());
		cardInfo.setBindNo(cardDo.getBindCardNo());
		if (cardDo.getBindType() != null) {
			cardInfo.setBindType(cardDo.getBindType());
		}
		
		if (cardDo.getCardActType() != null) {
			cardInfo.setCardActType(cardDo.getCardActType());
		}
		
		cardInfo.setCardNo(cardDo.getCardNo());
		if (cardDo.getCardType() != null) {
			cardInfo.setCardType(cardDo.getCardType());	
		}
		
		if (cardDo.getIsMainCard() != null) {
			cardInfo.setIsMainCard(cardDo.getIsMainCard());
		}
		
		if (cardDo.getBankName() != null) {
			cardInfo.setBankName(cardDo.getBankName());
		}
		
		if (cardDo.getRealName() != null) {
			cardInfo.setRealName(cardDo.getRealName());
		}
		if(cardDo.getIdentityNo() != null){
			cardInfo.setIdentityNo(cardDo.getIdentityNo());
		}
		if(cardDo.getCardProvince() != null){
			cardInfo.setCardProvince(cardDo.getCardProvince());
		}
		if(cardDo.getCardCity() != null){
			cardInfo.setCardCity(cardDo.getCardCity());
		}
		if(cardDo.getCompanyName() != null){
			cardInfo.setCompanyName(cardDo.getCompanyName());
		}
		if(cardDo.getBankAddr() != null){
			cardInfo.setBankAddr(cardDo.getBankAddr());
		}
		cardInfo.setCreateTime(cardDo.getCreateTime());
		cardInfo.setModifyTime(cardDo.getModifyTime());
		
		cardInfo.setSwiftCode(cardDo.getSwiftCode());
		cardInfo.setMobNo(cardDo.getMobileNo());
		
		if(cardDo.getBindType() == BindCardType.BIND_QUICK_PAY.getValue() && StringUtil.isNotBlank(cardDo.getCardNo()) && RegexUtil.Number(cardDo.getCardNo())){
			com.sztx.pay.center.core.service.bean.BankInfo bInfo = bankUtilService.queryBankInfo(cardDo.getCardNo());
			if(bInfo != null){
				cardInfo.setPassClipherPay(bInfo.isPassClipherPay());
			}
		}
		
		
		return cardInfo;
	}

	@Override
	@SlaveQueryAnnotation
	public BindCardInfo findLastPayCardInfo(String userId,int type) {
		switch (type) {
		case 1:
			return transferBindInfo(accountService.findLastBindCardInfo(userId));
		case 2:
			return transferBindInfo(accountService.findLastWithdrawCardInfo(userId));
		case 3:
			return transferBindInfo(accountService.findLastChargeCardInfo(userId));
		default:
			throw new BusinessException("type参数有误");
		}
		
	}

	@Override
	@SlaveQueryAnnotation
	public Integer findLastPayType(String userId) {
		return accountService.findLastChannelId(userId);
	}
	
	
	private BindCardInfo transferBindInfo(UserBindCardDO cardDo){
		if(cardDo == null){
			return null;
		}
		BindCardInfo cardInfo = new BindCardInfo();
		cardInfo.setBankId(cardDo.getBankId());
		cardInfo.setBindNo(cardDo.getBindCardNo());
		if (cardDo.getBindType() != null) {
			cardInfo.setBindType(cardDo.getBindType());
		}
		
		if (cardDo.getCardActType() != null) {
			cardInfo.setCardActType(cardDo.getCardActType());
		}
		
		cardInfo.setCardNo(cardDo.getCardNo());
		if (cardDo.getCardType() != null) {
			cardInfo.setCardType(cardDo.getCardType());	
		}
		
		if (cardDo.getIsMainCard() != null) {
			cardInfo.setIsMainCard(cardDo.getIsMainCard());
		}
		
		if (cardDo.getBankName() != null) {
			cardInfo.setBankName(cardDo.getBankName());
		}
		
		if (cardDo.getRealName() != null) {
			cardInfo.setRealName(cardDo.getRealName());
		}
		if(cardDo.getIdentityNo() != null){
			cardInfo.setIdentityNo(cardDo.getIdentityNo());
		}
		cardInfo.setCreateTime(cardDo.getCreateTime());
		cardInfo.setModifyTime(cardDo.getModifyTime());
		
		cardInfo.setSwiftCode(cardDo.getSwiftCode());
		cardInfo.setMobNo(cardDo.getMobileNo());
		return cardInfo;
	}

//	@Override
//	public UserActInfoResponse queryUserActInfo(String userId,String actType) {
//		UserActDO user = userActMysqlDAO.findByUserId(userId, ActType.BALANCE_ACT.getCode() );
//		boolean isMch = false;
//		String actNo = userId;
//		if(user != null){
//			String channelActNo = user.getChannelActNo();
//			if(channelActNo == null){
//				throw new BusinessException("帐号有误,请重新输入!");
//			}
//			isMch = channelActNo.length() >= 10 ? true : false;
//			actNo = user.getChannelActNo();
//		}else if(userId.length() >= 10){
//			isMch = true;
//		}
//		if(isMch){
//			//商户号
//			return accountService.queryMchAct(userId,actType);
//		}else{
//			//普通用户
//			UserActInfoResponse response = accountService.queryUserAct(actNo,actType,userId);
//			response.setChannelActNo(actNo);
//			return response;
//		}
//	}
//
//	@Override
//	public UserAccountInfoResponse queryUserAccountInfo(String userId,
//			String actType) {
//		List<BindCardInfo> bindCardInfos = this.queryBindCardList(userId, 0);
//		UserActInfoResponse userActInfoResponse = this.queryUserActInfo(userId, actType);
//		UserStatusInfoVO userStatusInfoVO = accountService.queryUserStatusInfo(userId);
//		if(userStatusInfoVO == null) return null;
//		UserAccountInfoResponse response = new UserAccountInfoResponse();
//		response.setBindCardInfos(bindCardInfos);
//		response.setUserActInfoResponse(userActInfoResponse);
//		
//		UserAccountInfoResponse.UserStatusInfo userStatusInfo = new UserAccountInfoResponse().new UserStatusInfo();
//		userStatusInfo.setIdentityid(userStatusInfoVO.getIdentityid());
//		userStatusInfo.setIsAuth(userStatusInfoVO.getIsAuth());
//		userStatusInfo.setLoginPwdState(userStatusInfoVO.getLoginPwdState());
//		userStatusInfo.setMob(userStatusInfoVO.getMob());
//		userStatusInfo.setPayAccountState(userStatusInfoVO.getPayAccountState());
//		userStatusInfo.setPayPwdState(userStatusInfoVO.getPayPwdState());
//		userStatusInfo.setRealname(userStatusInfoVO.getRealname());
//		userStatusInfo.setPlatformGroupName(userStatusInfoVO.getPlatformGroupName());
//		userStatusInfo.setRegisterPlatformId(userStatusInfoVO.getRegisterPlatformId());
//		userStatusInfo.setRegisterRole(userStatusInfoVO.getRegisterRole());
//		userStatusInfo.setUserCode(userStatusInfoVO.getUserCode());
//		userStatusInfo.setUserRole(userStatusInfoVO.getUserRole());
//		
//		response.setUserStatusInfo(userStatusInfo);
//		return response;
//	}
//	@Override
//	public Integer queryAgentActCount(String userId) {
//		UserActDO userActDO = new UserActDO();
//		userActDO.setUserType(0);
//		if(userId != null && userId != ""){
//			userActDO.setUserId(userId);
//		}
//		return userActMysqlDAO.listCount(userActDO);
//	}
//	
//	@Override
//	public List<UserActInfoResponse> queryAgentActList(String userId, int pageNo, int pageSize) {
//		PageQuery pageQuery = new PageQuery(pageNo, pageSize);
//		UserActDO userActDO = new UserActDO();
//		userActDO.setUserType(0);
//		if(userId != null && userId != ""){
//			userActDO.setUserId(userId);
//		}
//		List<UserActDO> listPage = userActMysqlDAO.listPage(userActDO  , pageQuery);
//		if(listPage == null){
//			return null;
//		}
//		List<UserActInfoResponse> response = new ArrayList<UserActInfoResponse>();
//		for (UserActDO userActDO2 : listPage) {
//			UserActInfoResponse userActInfoResponse = accountService.queryMchAct(userActDO2.getUserId(),"CASH");
//			response.add(userActInfoResponse);
//		}
//		return response;
//	}
//		
	@Override
	public boolean checkUserValid(String userId) {
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		
		return accountService.isGrayUser(userId);
//		if(grayUserList.contains(userId)){
//			return true;
//		}
//		
//		UserInfoDO userInfoDO = userInfoMysqlDAO.findByUserId(userId);
//		if(userInfoDO != null && userInfoDO.getGrayState() == 1){
//			grayUserList.add(userId);
//			return true;
//		}else{
//			return false;
//		}
	}


	@Override
	public void updateMerCard(String userId,String bindCardNo, BindCardRequest request) {
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		ValidateUtil.isNotBlank(bindCardNo, "bindCardNo不能为空");
		
		UserBindCardDO cardInfo = userBindCardMysqlDAO.findByUserCardNo(bindCardNo);
		if(cardInfo == null){
			throw new BusinessException("用户绑定号["+bindCardNo+"]不存在");
		}
		
		BindCardType bindType = BindCardType.valueOf(request.getBindType());		
		CardActType cardActType = CardActType.valueOf(request.getCardActType());
		CardType cardType = CardType.valueOf(request.getCardType());
		String cardNo = request.getCardNo();
		cardNo = YHBFSensitiveEnc.sensitiveEnc(cardNo,"cardno");
		String cvv = request.getCvv();
		String expireDate = request.getExpireDate();
		String realName = request.getRealName();
		String identityNo = request.getIdentityNo();
		String mobileNo = request.getMobileNo();
		String cardProvince = request.getCardProvince();
		String cardCity = request.getCardCity();
		String bankAddr = request.getBankAddr();
		String autoRemark = request.getAutoRemark();
		String companyName = request.getCompanyName();
		String updateUser = request.getUpdateUser();
		Integer state = request.getState();
		String bankName = request.getBankName();
		String swiftCode = request.getSwiftCode();
		

		BindInfo bindInfo = new BindInfo();
		if(bindType != null)
			bindInfo.setBindType(bindType);
		if(cardActType != null)
			bindInfo.setCardActType(cardActType);
		if(cardType != null)
			bindInfo.setCardType(cardType);
		if(cardNo != null)
			bindInfo.setCardNo(cardNo);
		if(cvv != null)
			bindInfo.setCvv(cvv);
		if(expireDate != null)
			bindInfo.setExpireDate(expireDate);
		if(realName != null)
			bindInfo.setRealName(realName);
		if(identityNo != null)
			bindInfo.setIdentityNo(identityNo);
		if(mobileNo != null)
			bindInfo.setMobileNo(mobileNo);
		if(cardProvince != null)
			bindInfo.setCardProvince(cardProvince);
		if(cardCity != null)
			bindInfo.setCardCity(cardCity);
		if(bankAddr != null)
			bindInfo.setBankAddr(bankAddr);
		if(autoRemark != null)
			bindInfo.setAutoRemark(autoRemark);
		if(companyName != null)
			bindInfo.setCompanyName(companyName);
		if(updateUser != null)
			bindInfo.setUpdateUser(updateUser);
		if(state != null)
			bindInfo.setStatus(state);
		if(bankName != null){
			bindInfo.setBankName(bankName);
		}
		if(swiftCode != null){
			bindInfo.setSwiftCode(swiftCode);
		}
		accountService.updateMchBindInfo(userId, bindCardNo, bindInfo);
	}
	
//	@Override
//	public ResponsePageModel<UserActInfoResponse> queryUserActList(Integer userType,
//			int actType, String userId, int pageNo, int pageSize) {
//		//ValidateUtil.ge(userType, 0, "用户类别userType参数错误");
//		ValidateUtil.ge(actType, 1, "帐户类型actType参数错误");
//		PageQuery pageQuery = new PageQuery(pageNo, pageSize);
//		return accountService.queryUserActList(userType, actType, userId, pageQuery);
//	}

//	@Override
//	public void modifyUserAct(String userId, String remark, int actState,
//			String lastOperator, int actType) {
//		ValidateUtil.ge(actType, 1, "帐户类型actType参数错误");
//		//ValidateUtil.isNotBlank(remark, "备注remark不能为空");
//		ValidateUtil.isNotBlank(lastOperator, "操作人lastOperator不能为空");
//		ValidateUtil.isNotBlank(userId, "用户帐号userId不能为空");
//		ValidateUtil.isBetween(actState, 1, 3, "账户状态actState错误,范围1~3");
//		accountService.modifyUserAct(userId, remark, actState, lastOperator, actType);
//	}
	
	private List<BindCardInfo> transferBindResult(List<UserBindCardDO> list){
		if(list == null){
			return null;
		}
		List<BindCardInfo> resultList = new ArrayList<BindCardInfo>();
		for (UserBindCardDO cardDo : list) {
			BindCardInfo cardInfo = new BindCardInfo();
			cardInfo.setBankId(cardDo.getBankId());
			cardInfo.setBindNo(cardDo.getBindCardNo());
			if (cardDo.getBindType() != null) {
				cardInfo.setBindType(cardDo.getBindType());
			}
			
			if (cardDo.getCardActType() != null) {
				cardInfo.setCardActType(cardDo.getCardActType());
			}
			
			cardInfo.setCardNo(cardDo.getCardNo());
			if (cardDo.getCardType() != null) {
				cardInfo.setCardType(cardDo.getCardType());	
			}
			
			if (cardDo.getIsMainCard() != null) {
				cardInfo.setIsMainCard(cardDo.getIsMainCard());
			}
			
			if (cardDo.getBankName() != null) {
				cardInfo.setBankName(cardDo.getBankName());
			}
			
			if (cardDo.getRealName() != null) {
				cardInfo.setRealName(cardDo.getRealName());
			}
			
			if(cardDo.getIdentityNo() != null){
				cardInfo.setIdentityNo(cardDo.getIdentityNo());
			}
			if(cardDo.getUserId() != null){
				cardInfo.setUserId(cardDo.getUserId());
			}
			if(cardDo.getCardProvince() != null){
				cardInfo.setCardProvince(cardDo.getCardProvince());
			}
			if(cardDo.getCardCity() != null){
				cardInfo.setCardCity(cardDo.getCardCity());
			}
			if(cardDo.getBankAddr() != null){
				cardInfo.setBankAddr(cardDo.getBankAddr());
			}
			
			if(cardDo.getAutoRemark() != null){
				cardInfo.setAutoRemark(cardDo.getAutoRemark());
			}
			if(cardDo.getCompanyName() != null){
				cardInfo.setCompanyName(cardDo.getCompanyName());
			}
			if(cardDo.getOperator() != null){
				cardInfo.setUpdateUser(cardDo.getOperator());
			}
			
			cardInfo.setCreateTime(cardDo.getCreateTime());
			cardInfo.setModifyTime(cardDo.getModifyTime());
			
			cardInfo.setSwiftCode(cardDo.getSwiftCode());
			cardInfo.setMobNo(cardDo.getMobileNo());
			
			if(cardDo.getBindType() == BindCardType.BIND_QUICK_PAY.getValue() && StringUtil.isNotBlank(cardDo.getCardNo()) && RegexUtil.Number(cardDo.getCardNo())){
				com.sztx.pay.center.core.service.bean.BankInfo bInfo = bankUtilService.queryBankInfo(cardDo.getCardNo());
				if(bInfo != null){
					cardInfo.setPassClipherPay(bInfo.isPassClipherPay());
				}
			}
			
			
			
			
			resultList.add(cardInfo);
		}
		return resultList;
	}

	@Override
	@SlaveQueryAnnotation
	public List<QueryBanlanceListResponse> queryBanlanceList(List<String> userIds,Integer actType) {
		LOG.debug("queryBanlanceList");
		UserActType userActType = UserActType.valueOf(actType);	
		ValidateUtil.notNull(userActType, "账号类别参数有误");
		List<UserActDO> userActDOS = actService.findUsersAct(userIds, userActType);
		List<QueryBanlanceListResponse> reponse = new ArrayList<QueryBanlanceListResponse>();
		
		
		for (UserActDO userAct : userActDOS) {
			QueryBanlanceListResponse tmpResponse = new QueryBanlanceListResponse();
			int actState = userAct.getActState(); 
			String userId = userAct.getUserId();
			tmpResponse.setUserId(userId);
			if(actState != 1){
				String msg = "用户"+userId+"状态异常";
				tmpResponse.setBanlance(0);
				tmpResponse.setQryFlag(false);
				tmpResponse.setMsg(msg);
			}else{
				tmpResponse.setBanlance(userAct.getActBalance());
				tmpResponse.setQryFlag(true);
			}
			reponse.add(tmpResponse);
		}
		
		for (String userId : userIds) {
			boolean exsit = false;
			for (QueryBanlanceListResponse tmpResponse : reponse) {
				if(tmpResponse.getUserId().equals(userId)){
					exsit = true;
				}
			}
			if(exsit == false){
				QueryBanlanceListResponse tmpResponse = new QueryBanlanceListResponse();
				tmpResponse.setBanlance(0);
				tmpResponse.setMsg("未查询到该账户");
				tmpResponse.setQryFlag(false);
				tmpResponse.setUserId(userId);
				reponse.add(tmpResponse);
			}
		}
		
		
		return reponse;
	}

	@Override
	@SlaveQueryAnnotation
	public UserAccountInfoResponse queryUserAccountInfo(String userId) {
		//List<BindCardInfo> bindCardInfos = this.queryBindCardList(userId, 0);
//		UserActInfoResponse userActInfoResponse = this.queryUserActInfo(userId, actType);
		UserStatusInfoVO userStatusInfoVO = accountService.queryUserStatusInfo(userId);
		if(userStatusInfoVO == null) return null;
		
		
		UserInfoDO userInfoDO = userInfoService.findUserInfo(userId);
		if(userInfoDO == null) return null;
		
		List<UserBindCardDO> userBindCardDOS = accountService.queryBindCardList(userId, 0);
		List<BindCardInfo> bindCardInfos = transferBindResult(userBindCardDOS);
		UserActInfoResponse userActInfoResponse = queryUserActInfo(userId);
		
		UserAccountInfoResponse response = new UserAccountInfoResponse();
		response.setBindCardInfos(bindCardInfos);
		response.setUserActInfoResponse(userActInfoResponse);
		
		UserAccountInfoResponse.UserStatusInfo userStatusInfo = new UserAccountInfoResponse().new UserStatusInfo();
		userStatusInfo.setIdentityid(userStatusInfoVO.getIdentityid());
		userStatusInfo.setIsAuth(userStatusInfoVO.getIsAuth());
		userStatusInfo.setLoginPwdState(userStatusInfoVO.getLoginPwdState());
		userStatusInfo.setMob(userStatusInfoVO.getMob());
		userStatusInfo.setPayAccountState(userStatusInfoVO.getPayAccountState());
		userStatusInfo.setPayPwdState(userStatusInfoVO.getPayPwdState());
		userStatusInfo.setRealname(userStatusInfoVO.getRealname());
		userStatusInfo.setPlatformGroupName(userStatusInfoVO.getPlatformGroupName());
		userStatusInfo.setRegisterPlatformId(userStatusInfoVO.getRegisterPlatformId());
		userStatusInfo.setRegisterRole(userStatusInfoVO.getRegisterRole());
		userStatusInfo.setUserCode(userStatusInfoVO.getUserCode());
		userStatusInfo.setUserRole(userStatusInfoVO.getUserRole());
		
		response.setUserStatusInfo(userStatusInfo);
		return response;
	}

	@Override
	@SlaveQueryAnnotation
	public Integer queryAgentActCount(String userId) {
		UserInfoDO userInfoDO = new UserInfoDO();
		userInfoDO.setUserType(UserType.SZTX.getCode());
		if(userId != null && userId != ""){
			userInfoDO.setUserId(userId);
		}
		return userInfoMysqlDAO.findPlatUserCount(userInfoDO);
	}

	@Override
	@SlaveQueryAnnotation
	public List<UserActInfoResponse> queryAgentActList(String userId,
			int pageNo, int pageSize) {
		PageQuery pageQuery = new PageQuery(pageNo, pageSize);
		UserInfoDO userInfoDO = new UserInfoDO();
		userInfoDO.setUserId(userId);
		userInfoDO.setUserType(UserType.SZTX.getCode());
		Integer counts = userInfoMysqlDAO.findPlatUserCount(userInfoDO);
		ResponsePageModel<UserActInfoResponse> rep = new ResponsePageModel<UserActInfoResponse>();
		rep.setTotal(counts);
		List<UserActInfoResponse> userActInfoResponseS= new ArrayList<UserActInfoResponse>();
		if(counts > 0){
			List<PlatUserInfoDO> userInfoDOS = userInfoMysqlDAO.findPlatUserList(userInfoDO, pageQuery);
			for (PlatUserInfoDO user : userInfoDOS) {
				UserActInfoResponse response = new UserActInfoResponse();
				int actState = user.getUserStatus(); 
				response.setActStatus(actState);
				response.setUserType(user.getUserType());
				response.setCreateTime(user.getCreateTime());
				response.setModifyTime(user.getUpdateTime());
				response.setUserId(user.getUserId());
				response.setUserRemark(user.getRemark());
				response.setLastOperator(user.getUpdateUser());
				response.setActLevel(user.getGrade());
				response.setRealName(user.getRealName());
				response.setIsRealName(user.getIsRealName());
				response.setGrayState(user.getGrayState());
				response.setBalance(user.getBalance());
				response.setFreezedBalance(user.getFreezedBalance());
				userActInfoResponseS.add(response);
			}
			rep.setResultLists(userActInfoResponseS);
		}
		return userActInfoResponseS;
	}

	@Override
	@SlaveQueryAnnotation
	public ResponsePageModel<BindCardInfo> queryMerCardList(
			QueryMerBindCardRequest request) {
		ResponsePageModel<BindCardInfo> reponse = new ResponsePageModel<BindCardInfo>();
		com.sztx.pay.center.core.service.bean.rpc.QueryMerBindCardRequest req = new com.sztx.pay.center.core.service.bean.rpc.QueryMerBindCardRequest();
		req = ObjectUtil.copyProperties(request, req);
		com.sztx.pay.center.core.service.bean.rpc.ResponsePageModel<UserBindCardDO> mchBindCardInfos = accountService.queryMchCardList(req);
		//ResponsePageModel<UserBindCardDO> mchBindCardInfos = accountService.queryMchCardList(req);
		List<BindCardInfo> bindCardInfoS = transferBindResult(mchBindCardInfos.getResultLists());
		reponse.setResultLists(bindCardInfoS);
		reponse.setTotal(mchBindCardInfos.getTotal());
		return reponse;
	}
	/**
	 *  userType: 
	 *  SZTX(1, "神州通行", 6),
		PERSON(2,"个人客户", 2),
		PARTNER(4,"商户", 6),
		AGENT(8, "代理商", 10),
		MPOS(16, "Mpos", 10);
	 * @param userType
	 * @param userId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@Override
	@SlaveQueryAnnotation
	public ResponsePageModel<UserActInfoResponse> queryUserActList(
			String userId,String mob,String identifyNo,String realName, int pageNo,
			int pageSize) {
		ValidateUtil.gt(pageNo, 0, "pageNo不能小于0");
		ValidateUtil.gt(pageSize, 0, "pageSize不能小于0");
		PageQuery pageQuery = new PageQuery(pageNo, pageSize);

		
		UserInfoDO userInfoDO = new UserInfoDO();
		userInfoDO.setUserId(userId);
		userInfoDO.setMobile(mob);
		userInfoDO.setIdentityNo(identifyNo);
		userInfoDO.setRealName(realName);
		Integer counts = userInfoMysqlDAO.findUserListCount(userInfoDO);
		ResponsePageModel<UserActInfoResponse> rep = new ResponsePageModel<UserActInfoResponse>();
		rep.setTotal(counts);
		if(counts > 0){
			List<UserInfoDO> userInfoDOS = userInfoMysqlDAO.findUserList(userInfoDO, pageQuery);
			List<UserActInfoResponse> userActInfoResponseS= new ArrayList<UserActInfoResponse>();
			for (UserInfoDO user : userInfoDOS) {
				UserActInfoResponse response = new UserActInfoResponse();
				int actState = user.getUserStatus(); 
				response.setActStatus(actState);
				response.setUserType(user.getUserType());
				response.setCreateTime(user.getCreateTime());
				response.setModifyTime(user.getUpdateTime());
				response.setUserId(user.getUserId());
				response.setUserRemark(user.getRemark());
				response.setLastOperator(user.getUpdateUser());
				response.setActLevel(user.getGrade());
				response.setRealName(user.getRealName());
				response.setIsRealName(user.getIsRealName());
				response.setGrayState(user.getGrayState());
				userActInfoResponseS.add(response);
			}
			rep.setResultLists(userActInfoResponseS);
		}
		return rep;
	}

	@Override
	public void modifyUserAct(String userId, String remark, int actState,
			String lastOperator, int actType) {
		ValidateUtil.ge(actType, 1, "帐户类型actType参数错误");
		ValidateUtil.isNotBlank(lastOperator, "操作人lastOperator不能为空");
		ValidateUtil.isNotBlank(userId, "用户帐号userId不能为空");
		ValidateUtil.isBetween(actState, 1, 3, "账户状态actState错误,范围1~3");
		actService.updateUserAct(userId, remark, actState, lastOperator, actType);
		
	}
	@Override
	public void modifyUserInfo(String userId, String remark, int actState,
			String lastOperator) {
		ValidateUtil.isNotBlank(lastOperator, "操作人lastOperator不能为空");
		ValidateUtil.isNotBlank(userId, "用户帐号userId不能为空");
		ValidateUtil.isBetween(actState, 1, 3, "账户状态actState错误,范围1~3");
		UserInfoDO newUserInfo = userInfoService.findUserInfo(userId);
		newUserInfo.setUpdateUser(lastOperator);
		newUserInfo.setUserStatus(actState);
		newUserInfo.setRemark(remark);
		userInfoService.updateUserInfo(userId, newUserInfo );
	}
	@Override
	@SlaveQueryAnnotation
	public List<UserActInfoResponse> queryUserActInfo(String userId,List<Integer> userActTypeList) {
		List<UserActDO> userActList = userActMysqlDAO.findByUserIdAndActType(userId, userActTypeList);
		List<UserActInfoResponse> response = new ArrayList<UserActInfoResponse>();
		UserInfoDO userInfoDO = userInfoService.findUserInfo(userId);
		if(userInfoDO != null){
			for (UserActDO userActDO : userActList) {
				UserActInfoResponse userActInfo = new UserActInfoResponse();
				userActInfo.setActLevel(userActDO.getActLevel());
				userActInfo.setActStatus(userActDO.getActState());
				userActInfo.setBalance(userActDO.getActBalance());
				userActInfo.setActNo(userActDO.getActNo());
				userActInfo.setCreateTime(userActDO.getCreateTime());
				userActInfo.setFreezedBalance(userActDO.getFreezeBalance());
				userActInfo.setLastOperator(userActDO.getUpdateUser());
				userActInfo.setModifyTime(userActDO.getModifyTime());
				userActInfo.setNickName(userInfoDO.getRealName());
				userActInfo.setRealName(userInfoDO.getRealName());
				userActInfo.setUserActType(userActDO.getActType());
				userActInfo.setUserId(userId);
				userActInfo.setUserRemark(userInfoDO.getRemark());
				userActInfo.setUserType(userInfoDO.getUserType());
				response.add(userActInfo);
			}
		}
		
		return response;
	}

	@Override
	public void grayUser(String userId, String operator, String remark) {
		
		accountService.grayUser(userId, operator, remark);
		
	}

	@Override
	@SlaveQueryAnnotation
	public UserActInfoResponse queryUserActInfo(String userId) {
		UserInfoDO userInfoDO = userInfoService.findUserInfo(userId);
		Integer userType = userInfoDO.getUserType();
		boolean isJfUser = (userType.intValue() & Constance.UserType.AGENT.getCode()) != 0;
		UserActDO userActDO = null;
		UserActType userActType = null;
		if(isJfUser){
			userActType = UserActType.JF;
		}else{
			userActType = UserActType.CASH;
		}
		userActDO = actService.findUserAct(userId, userActType);
		if(userActDO == null){
			throw new BusinessException("未查询到用户【"+userId+"】的账户【"+userActType.getCode()+"】");
		}
		UserActInfoResponse userActInfo = new UserActInfoResponse();
		userActInfo.setActLevel(userActDO.getActLevel());
		userActInfo.setActStatus(userActDO.getActState());
		userActInfo.setBalance(userActDO.getActBalance());
		userActInfo.setActNo(userActDO.getActNo());
		userActInfo.setCreateTime(userActDO.getCreateTime());
		userActInfo.setFreezedBalance(userActDO.getFreezeBalance());
		userActInfo.setLastOperator(userActDO.getUpdateUser());
		userActInfo.setModifyTime(userActDO.getModifyTime());
		userActInfo.setNickName(userInfoDO.getRealName());
		userActInfo.setRealName(userInfoDO.getRealName());
		userActInfo.setUserActType(userActDO.getActType());
		userActInfo.setUserId(userId);
		userActInfo.setUserRemark(userInfoDO.getRemark());
		userActInfo.setUserType(userInfoDO.getUserType());
		return userActInfo;
	}

	@Override
	public boolean freezeMoney(String userId, Integer actType, long amount,
			String operator, String attach) {
		
		LOG.debug("freezeMoney");
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		ValidateUtil.isNotBlank(operator, "operator不能为空");
		ValidateUtil.notNull(actType, "actType不能为空");
		ValidateUtil.gt(amount, 0l, "冻结金额必须大于0");
		
		if (StringUtil.isNotBlank(attach)) {
			ValidateUtil.le(attach.length(), 255, "attach不能超过255个字符");
		}
		
		return accountService.freezeMoney(userId, actType, amount, operator, attach);
	}

	@Override
	public boolean unfreezeMoney(String userId, Integer actType, long amount,
			String operator, String attach) {
		LOG.debug("unfreezeMoney");
		ValidateUtil.isNotBlank(userId, "userId不能为空");
		ValidateUtil.isNotBlank(operator, "operator不能为空");
		ValidateUtil.notNull(actType, "actType不能为空");
		ValidateUtil.gt(amount, 0l, "冻结金额必须大于0");
		
		if (StringUtil.isNotBlank(attach)) {
			ValidateUtil.le(attach.length(), 255, "attach不能超过255个字符");
		}
		
		return accountService.unfreezeMoney(userId, actType, amount, operator, attach);
	}

	@Override
	public boolean syncUserBalance(String userId) {
		return accountService.syncUserBalance(userId);
	}

	@Override
	public boolean realNameTfAct(String userId, String realName,
			String identityNo, String mobileNo) {
		return accountService.realNameTfAct(userId, realName, identityNo, mobileNo);
	}

	@Override
	public boolean changeGrayState(String userId, Integer newGraySate) {
		return accountService.changeGrayState(userId, newGraySate);
	}

	@Override
	public boolean syncBindCard(String userId) {
		return accountService.syncBindCard(userId);
	}

	@Override
	@SlaveQueryAnnotation
	public UserActInfo queryUserActAllInfo(String userId) {
		UserActInfo actInfo = new UserActInfo();
		UserInfoDO userInfoDO = userInfoService.findUserInfo(userId);
		if(userInfoDO == null) return null;
		List<Integer> userActTypeList = Arrays.asList(new Integer[]{2,4,8});
		List<UserActDO> userActList = userActMysqlDAO.findByUserIdAndActType(userId, userActTypeList);
		List<UserActInfoResponse> userActInfos = new ArrayList<UserActInfoResponse>();
		
		actInfo.setIdentityid(userInfoDO.getIdentityNo());
		actInfo.setIsAuth(userInfoDO.getIsRealName());
		actInfo.setMob(userInfoDO.getMobile());
		actInfo.setUserCode(userInfoDO.getUserId());
		actInfo.setUserRole(userInfoDO.getUserType());
		boolean isSZTXUser = (userInfoDO.getUserType() & Constance.UserType.SZTX.getCode()) != 0;
		if(isSZTXUser){
			String realName = "平台中间账户【"+userInfoDO.getRemark()+"】";
			actInfo.setRealname(realName);
		}else{
			actInfo.setRealname(userInfoDO.getRealName());
		}
		
		
		for (UserActDO userActDO : userActList) {
			UserActInfoResponse userActInfo = new UserActInfoResponse();
			userActInfo.setActLevel(userActDO.getActLevel());
			userActInfo.setActStatus(userActDO.getActState());
			userActInfo.setBalance(userActDO.getActBalance());
			userActInfo.setActNo(userActDO.getActNo());
			userActInfo.setCreateTime(userActDO.getCreateTime());
			userActInfo.setFreezedBalance(userActDO.getFreezeBalance());
			userActInfo.setLastOperator(userActDO.getUpdateUser());
			userActInfo.setModifyTime(userActDO.getModifyTime());
			userActInfo.setNickName(userInfoDO.getRealName());
			userActInfo.setRealName(userInfoDO.getRealName());
			userActInfo.setUserActType(userActDO.getActType());
			userActInfo.setUserId(userId);
			userActInfo.setUserRemark(userInfoDO.getRemark());
			userActInfo.setUserType(userInfoDO.getUserType());
			userActInfos.add(userActInfo);
		}
		actInfo.setUserActInfos(userActInfos);
		return actInfo;
	}

	@Override
	@SlaveQueryAnnotation
	public PageModelResponse<UserInfoResponse> queryUserActList(
			UserInfoRequest request,int pageNo,int pageSize) {
		PageModelResponse<UserInfoResponse> response = new PageModelResponse<UserInfoResponse>();
		com.sztx.pay.center.core.service.bean.rpc.UserInfoRequest req = new com.sztx.pay.center.core.service.bean.rpc.UserInfoRequest();
		req = ObjectUtil.copyProperties(request, req);
		Integer count = accountService.queryUserActCount(req );
		response.setPageNo(pageNo);
		response.setPageSize(pageSize);
		response.setTotal(count);
		if(count > 0){
			List<com.sztx.pay.center.core.service.bean.rpc.UserInfoResponse> userInfoList = accountService.queryUserActList(req, pageNo, pageSize);
			List<UserInfoResponse> userInfoResponseList = new ArrayList<UserInfoResponse>();
//			for (com.sztx.pay.center.core.service.bean.rpc.UserInfoResponse userInfoRes : userInfoList) {
//				UserInfoResponse tmp = new UserInfoResponse();
//				tmp = ObjectUtil.copyProperties(userInfoRes, tmp);
//				userInfoResponseList.add(tmp);
//			}
			userInfoResponseList = ObjectUtil.copyProperties(userInfoList, userInfoResponseList);
			response.setList(userInfoResponseList);
		}
		return response;
	}

}
